package com.ibm.spatiotemp.server.datasources.ifx;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.Collection;
import java.util.Iterator;

import org.apache.log4j.Logger;


import com.ibm.spatiotemp.server.datamodel.gpsdata.TSPoint;
import com.ibm.spatiotemp.server.datamodel.gpsdata.TSPointResultSet;
import com.ibm.spatiotemp.server.datamodel.gpsdata.ifx.InformixTSPointResultSet;
import com.ibm.spatiotemp.server.datasources.DataSchema;
import com.ibm.spatiotemp.server.datasources.DataSource;
import com.ibm.spatiotemp.server.datasources.RawDataAccessor;
import com.ibm.spatiotemp.server.framework.request.Request;
import com.ibm.spatiotemp.tools.CoreMessages;
import com.ibm.spatiotemp.utils.ConfigManager;
import com.ibm.spatiotemp.utils.DBThreadPool;

public class InformixRawDataAccessor extends RawDataAccessor{
	public final static Logger logger = Logger.getLogger(InformixRawDataAccessor.class);
	
	DBThreadPool connPool = null;
	SQLException connectException = null;
	Connection conn = null;

	public InformixRawDataAccessor(
			DataSource src) {
		super(src);
		try {
			connPool = new DBThreadPool(src, ConfigManager.DB_THREAD_NUMBER,ConfigManager.MAX_QUEUE_LENGTH);
			conn = connPool.newConnection();
			connPool.startConsumers();
		} catch (SQLException e) {
			connectException = e;
		}
		queryformatter = new InformixQueryStringFormatter();
	}

	@Override
	public void connect() throws Exception {
		if (connectException != null) {
			throw connectException;
		}
		if (conn == null) {
			conn = connPool.newConnection();
		}		
	}

	@Override
	public void close() throws Exception {
		if (conn != null) {
			conn.close();
			conn = null;
		}
		connPool.stopConsumers();
	}

	@Override
	public TSPointResultSet ScanGPSData(Request request) throws Exception{
		if (connectException != null) {
			throw connectException;
		}
		Statement st = null;
		ResultSet resultSet = null;
		String sqlStr = null;
		InformixTSPointResultSet tsResultSet = null;
		String temp = null;
		DataSchema schema = DataSource.getDataschema();
		
		String modid = request.getId();
		String startts = request.getStartts();
		String endts = request.getEndts();
		temp = startts.replaceFirst("T", " ");
		try {
			Timestamp.valueOf(temp).getTime();
		} catch (IllegalArgumentException e) {
			throw new Exception(String.format(CoreMessages.STHS0001E, "start", startts));
		}
		startts = "\'" + temp + "\'";
		temp = endts.replaceFirst("T", " ");
		try {
			Timestamp.valueOf(temp).getTime();
		} catch (IllegalArgumentException e) {
			throw new Exception(String.format(CoreMessages.STHS0001E, "end", endts));
		}
		endts = "\'" + temp + "\'";
		
		sqlStr = queryformatter.RawDataScanQueryString(schema, startts, endts, modid);		
		
		st = conn.createStatement();
		resultSet = st.executeQuery(sqlStr);
		tsResultSet = new InformixTSPointResultSet(resultSet, st, modid);
		return tsResultSet;
	}

	@Override
	public int WriteGPSData(String write_string,
			Collection<TSPoint> tsp_data) throws Exception {
		if (connectException != null) {
			throw connectException;
		}	
		int size = tsp_data.size();
		if (size == 0) {
			return 0;
		}
		String modid = null;
		Iterator<TSPoint> iter = tsp_data.iterator();
		TSPoint tsPoint = null;
		// process the first one
		tsPoint = iter.next();
		modid = tsPoint.tsid;
		connPool.put(modid, tsp_data);
		return size;
	}
	

	@Override
	public TSPointResultSet ScanGPSData(String query_string) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

}
